package com.google.code.chaotic.client.ui;

import com.google.gwt.dom.client.Style.Float;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.geocode.DirectionsPanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SplitLayoutPanel;

public final class RouteConfirmDialog extends DialogBox {

	public static enum Result {
		CONFIRM, MODIFY, CANCEL
	}

	private Result result = null;

	//

	public RouteConfirmDialog( //
			MapWidget mapWidget, DirectionsPanel directionsPanel) {

		super(false, true);

		this.setText("Route Confirmation");
		this.setGlassEnabled(true);
		this.setAnimationEnabled(true);

		FlowPanel vPanel = new FlowPanel();
		// vPanel.setSpacing(10);
		Label label = new Label(
				"The following was extracted from the link you provided."
						+ "\nPlease check if it is correct...");
		vPanel.add(label);
		SplitLayoutPanel hSplit = new SplitLayoutPanel();
		hSplit.setSize("880px", "440px");
		// directionsPanel.setSize("200px", "400px");
		mapWidget.setSize("100%", "100%");
		hSplit.addWest(directionsPanel, 300);
		hSplit.add(mapWidget);
		vPanel.add(hSplit);
		// vPanel.setCellHeight(hSplit, "100%");
		FlowPanel buttonsPanel = new FlowPanel();

		final Button confirmButton = new Button("Confirm");
		final Button modifyButton = new Button("Modify");
		final Button cancelButton = new Button("Cancel");

		ClickHandler handler = new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Object source = event.getSource();
				if (source == confirmButton) {
					result = Result.CONFIRM;

				} else if (source == modifyButton) {
					result = Result.MODIFY;

				} else {
					result = Result.CANCEL;
				}
				hide();
			}
		};
		confirmButton.addClickHandler(handler);
		modifyButton.addClickHandler(handler);
		cancelButton.addClickHandler(handler);

		buttonsPanel.add(cancelButton);
		buttonsPanel.add(modifyButton);
		buttonsPanel.add(confirmButton);

		vPanel.add(buttonsPanel);
		this.setWidget(vPanel);

		label.getElement().getStyle().setMargin(0.75, Unit.EM);

		buttonsPanel.getElement().getStyle().setMarginTop(0.75, Unit.EM);
		buttonsPanel.getElement().getStyle().setMarginBottom(0.75, Unit.EM);

		confirmButton.getElement().getStyle().setFloat(Float.RIGHT);
		modifyButton.getElement().getStyle().setFloat(Float.RIGHT);
		cancelButton.getElement().getStyle().setFloat(Float.RIGHT);

		confirmButton.getElement().getStyle().setMarginRight(0.75, Unit.EM);
		modifyButton.getElement().getStyle().setMarginRight(0.75, Unit.EM);
		cancelButton.getElement().getStyle().setMarginRight(0.75, Unit.EM);
	}

	public Result getResult() {
		return result;
	}
}